package com.fzb.zrlog.simple.controller;

import com.fzb.common.util.Md5Util;
import com.fzb.zrlog.simple.dao.api.IUserDAO;
import com.fzb.zrlog.simple.dao.impl.Comment;
import com.fzb.zrlog.simple.dao.impl.Log;
import com.fzb.zrlog.simple.dao.impl.User;
import com.hibegin.http.server.api.HttpRequest;
import com.hibegin.http.server.api.HttpResponse;
import com.hibegin.http.server.web.cookie.Cookie;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class UserControl extends ManageControl {

    private IUserDAO userDAO;

    public UserControl(HttpRequest request, HttpResponse response) {
        super(request, response);
        userDAO = new User();

        String basePath = request.getScheme() + "://" + request.getHeader("Host") + "/" + "admin/";
        request.getAttr().put("suburl", request.getFullUrl().substring(basePath.length()));
    }

    public void delete() {

    }

    public void queryAll() {

    }

    public void index() {
        if (getSessionAttr("user") != null) {
            getSession().setAttr("comments", new Comment().noRead(1, 5));
            getSession().setAttr("commCount", new Comment().getCommentCount());
            getSession().setAttr("toDayCommCount", new Comment().getToDayCommentCount());
            getSession().setAttr("clickCount", new Log().getAllClick());
            if (getPara("redirectFrom") != null
                    && !"".equals(getPara("redirectFrom"))) {
                getResponse().redirect(getPara("redirectFrom"));
                return;
            }
            if ("login".equals(getPara(0))) {
                getResponse().renderFreeMarker("/admin/index");
            } else {
                getResponse().renderFreeMarker("/admin/" + getPara(0));
            }
        } else {
            getResponse().renderFreeMarker("/admin/login");
        }
    }

    public void logout() {

    }

    private String getBaseMs() {
        return null;
    }

    public void login() {
        boolean login = false;
        if (getCookie("zId") != null) {
            Map<String, Map> userMap = (Map<String, Map>) getWebContext().get("userMap");
            if (userMap != null) {
                String zId = getBaseMs() + getCookie("zId");
                Map user = userMap.get(zId);
                if (user != null) {
                    user = userDAO.login(user.get("userName").toString(), user.get("password").toString());
                    if (user != null) {
                        getSession().setAttr("user", user);
                        login = true;
                    }
                }
            }
        }
        if (!login && getPara("userName") != null && getPara("password") != null) {
            Map user = userDAO.login(getPara("userName"), Md5Util.MD5(getPara("password")).toUpperCase());
            if (user != null) {
                getSession().setAttr("user", user);
                if ("on".equals(getPara("rememberMe"))) {
                    Map<String, Object> userMap = (Map<String, Object>) getWebContext().get("userMap");
                    if (userMap == null) {
                        userMap = new HashMap();
                        getWebContext().put("userMap", userMap);
                    }
                    String zid = UUID.randomUUID().toString();
                    Cookie cookie = new Cookie(true);
                    cookie.setName("zid");
                    cookie.setValue(zid);
                    cookie.setMaxAge(60 * 60 * 24 * 30);
                    //cookie.setHttpOnly(true);
                    String host = getRequest().getHeader("Host");
                    int idx = host.indexOf(":");
                    if (idx != -1) {
                        host = host.substring(0, idx);
                    }
                    cookie.setDomain(host);
                    cookie.setPath("/");
                    getResponse().addCookie(cookie);
                    userMap.put(getBaseMs() + zid, user);
                }
            } else {
                setAttr("errorMsg", "用户名或密码错误");
            }
        }
        index();

    }

    @Override
    public void add() {

    }

    @Override
    public void update() {

    }

    public void changePassword() {

    }

}
